﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using UTSDAL.LINQSQL.DAO;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.Views;
using System.Text;

public partial class pages_admin_reservation_List : System.Web.UI.Page
{
    private static readonly ILog log = LogManager.GetLogger(typeof(pages_admin_reservation_List));

    private List<Semester> semesterList = null;

    private SemesterDAO semesterDAO = new SemesterDAO();

    private Semester selectedSemester = null;

    private TimetableEventDAO timetableEventDAO = new TimetableEventDAO();

    private List<ReservationView> timetableEvents = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Getting current Semester as per current date
        this.selectedSemester = semesterDAO.getCurrentSemester();

        semesterList = semesterDAO.selectAll();
        SemesterListDropDown.DataSource = semesterList;
        SemesterListDropDown.DataBind();

        // Setting Current Semester as Default value to be displayed
        SemesterListDropDown.SelectedValue = selectedSemester.SemesterId.ToString();

        timetableEvents = timetableEventDAO.getAllReservationViews(selectedSemester);
        ReservationGridView.DataSource = timetableEvents;
        ReservationGridView.DataBind();
    }
    

    private ReservationView selectedTimetableEvent = null;

    protected void ReservationEdit(object sender, EventArgs e)
    {
        LinkButton EditLinkButtonControl = (LinkButton)sender;

        String timetableEventId = EditLinkButtonControl.CommandArgument.ToString();

        log.Debug("Editing Reservation With Id " + timetableEventId);
        EditReservation(timetableEventId);

    }


    protected void ReservationGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        log.Debug("Row Created");
        ReservationView reservationView = (ReservationView)e.Row.DataItem;
        
        if (reservationView != null)
        {
            log.Debug("Setting Cohort & Module");
            Literal CohortLiteralControl = (Literal)e.Row.FindControl("Cohort");
            CohortLiteralControl.Text = reservationView.Cohort.CohortName;

            Literal ModuleLiteralControl = (Literal)e.Row.FindControl("Module");
            ModuleLiteralControl.Text = reservationView.Module.ModuleName;

            GridView ModuleEventReservationGridControl = (GridView)e.Row.FindControl("ModuleEventReservationGrid");

            StringBuilder builder = new StringBuilder();

            builder.Append("<table cellspacing='5px' cellpadding='5px' border='0'>");
            foreach(TimetableEvent reservedEvent in reservationView.ReservedEvent)
            {
                builder.Append("<tr>");
                builder.Append("<td>");
                builder.Append(reservedEvent.ModuleEvent.ModuleEventType.ModuleEventTypeName);
                builder.Append("</td>");
                builder.Append("<td>");
                builder.Append(reservedEvent.CourseDay.CourseDayName);
                builder.Append("</td>");
                builder.Append("<td>");
                builder.Append(TimeUtils.convertTimespan(reservedEvent.StartTime));
                builder.Append(" - ");
                builder.Append(TimeUtils.convertTimespan(reservedEvent.EndTime));
                builder.Append("</td>");
                
                builder.Append("</tr>");
            }
            builder.Append("</table>");



            Literal ModuleEventInfoLiteralControl = (Literal)e.Row.FindControl("ModuleEventInfo");
            ModuleEventInfoLiteralControl.Text = builder.ToString();
                        
        }
    }

    
    private void EditReservation(String reservationViewId)
    {
        log.Debug("Enable Edit Mode " + reservationViewId);

        var selectedTimetableEventVar = from e in timetableEvents
                                        where e.ReservationViewId.Equals(reservationViewId)
                                        select e;

        selectedTimetableEvent = selectedTimetableEventVar.Single<ReservationView>();
        Session["selectedReservation"] = selectedTimetableEvent;

        Response.Redirect("Insert.aspx");
    }


    protected void ReservationNew(object sender, EventArgs e)
    {
        Session["selectedReservation"] = null;
        Response.Redirect("Insert.aspx");
    }


    protected void ReservationDelete(object sender, EventArgs e)
    {
        LinkButton DeleteButton = (LinkButton)sender;

        String objectToDeleteId = DeleteButton.CommandArgument.ToString();

        var selectedTimetableEventVar = from timetableEvent in timetableEvents
                                        where timetableEvent.ReservationViewId.Equals(objectToDeleteId)
                                        select timetableEvent;

        ReservationView view = selectedTimetableEventVar.Single<ReservationView>();

        foreach(TimetableEvent timetableEvent in view.ReservedEvent)
        {
            timetableEventDAO.remove(timetableEvent);
        }
    }


}
